<?php namespace app\api\controller;

use app\common\controller\AuthAndBind;
use think\Controller;
use think\Db;
use alisms\SendSms;
use esign\EsignHandle;

/**
 * api
 */
class ShoppingCart extends AuthAndBind{
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 详情
	 */
//	public function details(){
//		if(request() -> isPost()){
//			$in = input("post.");
//			$bool = is_empty(['user_id', 'id'], $in);
//			if($bool){
//				$outArr['code'] = '40004';
//			}else{
//				$userInfo = $this -> getUserInfo();
//				
//				$where[] = ['id', 'eq', $in['id']];
//				
//				$data = Db::table("gd_attribute") -> where($where) -> find();
//				
//				$outArr['code'] = '1';
//				$outArr['info'] = $data;
//				
////				写入操作日志
//				controller("service/OperLog") -> logs(
//					[
//						'title'=>'获取属性详情',
//						'business_type'=>4,
//						'oper_name'=>$userInfo['info']['nickname'],
//						'oper_user_id'=>$userInfo['info']['id'],
//						'json'=>[
//							[
//								'json_result'=>$outArr,
//								'old_data'=> isset($old_data_json) ? $old_data_json : [],
//							]
//						]
//					]
//				);
//			}
//		}else{
//			$outArr['code'] = '50001';
//		}
//		returnResults($outArr);
//	}
	
	/**
	 * 列表
	 */
	public function index(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where[] = ['is_delete', 'eq', 0];
				if( isset($in['create_user_id']) ) $where[] = ['create_user_id', 'eq', $in['create_user_id']];
				
				$data = Db::table("gd_shopping_cart") -> where($where) -> order("id desc") -> limit($start, $num) -> select();
				$count = Db::table("gd_shopping_cart") -> where($where) -> count();
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取购物车列表',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 编辑
	 */
	public function edit($in, $oldData){
		$userInfo = $this -> getUserInfo();
		
		if( empty($oldData) ) returnResults(['code'=>'-1', 'msg'=>'原数据有误']);
		
		$save = [];
		foreach ($in as $k => $v) {
			if( !in_array($k, ['id', 'user_id']) ){
				$save[$k] = is_array($v) ? json_encode($v, JSON_UNESCAPED_SLASHES) : $v;
			}
		}
		
		$save = filterArr($save, array_keys($save), $oldData);
		
		if( !empty($save) ){
			$re = Db::table("gd_shopping_cart") -> where(['id'=>$oldData['id']]) -> update($save);
			if( !$re ) returnResults2('-1', '编辑失败,请联系管理员');
		}
		$outArr['code'] = '1';
		
//				写入操作日志
		$old_data_json = $oldData;
		controller("service/OperLog") -> logs(
			[
				'title'=>'编辑购物车',
				'business_type'=>3,
				'oper_name'=>$userInfo['info']['nickname'],
				'oper_user_id'=>$userInfo['info']['id'],
				'json'=>[
					[
						'json_result'=>$outArr,
						'old_data'=> isset($old_data_json) ? $old_data_json : [],
					]
				]
			]
		);
		returnResults($outArr);
	}
	
	/**
	 * 新增
	 */
	public function add($in){
		$userInfo = $this -> getUserInfo();
		
		$newData = [];
		foreach ($in as $k => $v) {
			if( !in_array($k, ['id', 'user_id']) ){
				$newData[$k] = is_array($v) ? json_encode($v, JSON_UNESCAPED_UNICODE) : $v;
			}
			$newData['create_user_id'] = $in['user_id'];
			$newData['create_user_name'] = $userInfo['info']['nickname'];
			$newData['create_user_type'] = $userInfo['info']['type'];
			$newData['create_date'] = date("Y-m-d H:i:s", time());
		}
		$re = Db::table("gd_shopping_cart") -> insertGetId($newData);
		if( !$re ) returnResults2('-1', '新增失败,请联系管理员');
		$outArr['code'] = '1';
		$outArr['info'] = $re;
		
//				写入操作日志
		controller("service/OperLog") -> logs(
			[
				'title'=>'加入购物车',
				'business_type'=>1,
				'oper_name'=>$userInfo['info']['nickname'],
				'oper_user_id'=>$userInfo['info']['id'],
				'json'=>[
					[
						'json_result'=>$outArr,
						'old_data'=> isset($old_data_json) ? $old_data_json : [],
					]
				]
			]
		);
		returnResults($outArr);
	}

	/**
	 * 保存
	 */
	public function save(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where = [];
				$where[] = ['goods_id', 'eq', $in['goods_id']];
				$where[] = ['create_user_id', 'eq', $in['user_id']];
				$where[] = ['is_delete', 'eq', 0];
				$oldData = Db::table("gd_shopping_cart") -> where($where) -> find();
				if( empty($oldData) ) $re = $this -> add($in);
				if( !empty($oldData) ) $re = $this -> edit($in, $oldData);
				
				$outArr['code'] = '1';
				$outArr['info'] = $re;
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
}

?>